package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.DataStatusEnum;
import com.linkhub.oms.common.enums.mp.PlatFormEnum;
import com.linkhub.oms.common.enums.mp.SuccessfulEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mapper.ShopEntityMapper;
import com.linkhub.oms.dao.mpservice.IShopService;
import com.linkhub.oms.entity.ShopEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商店表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-20
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopEntityMapper, ShopEntity> implements IShopService {

    @Override
    public List<ShopEntity> listByUserId(String userId, SuccessfulEnum successfulEnum) {
        return lambdaQuery()
                .eq(ShopEntity::getUserId, userId)
                .eq(Objects.nonNull(successfulEnum), ShopEntity::getAuthorization, successfulEnum)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    public List<ShopEntity> listByUserId(String userId) {
        return this.listByUserId(userId, null);
    }

    @Override
    public List<String> listShopIds(String userId) {
        List<ShopEntity> list = lambdaQuery()
                .eq(ShopEntity::getUserId, userId)
                .list();
        return QtStreamUtil.map(list, ShopEntity::getSysShopId);
    }

    @Override
    public List<ShopEntity> listAllAuthorizationShops() {
        return lambdaQuery()
                .eq(ShopEntity::getAuthorization, SuccessfulEnum.YES)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .eq(ShopEntity::getPlatForm, PlatFormEnum.SHOPIFY)
                .list();
    }

    @Override
    public ShopEntity getUserShop(String userId, String sysShopId) {
        return lambdaQuery()
                .eq(ShopEntity::getUserId, userId)
                .eq(ShopEntity::getSysShopId, sysShopId)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .one();
    }

    @Override
    public ShopEntity getUserShopByShopName( String userId, String shopName ) {
        return lambdaQuery()
                .eq(ShopEntity::getUserId, userId)
                .eq(ShopEntity::getShopName, shopName)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .one();
    }

    @Override
    public boolean checkShopExist(String userId, String shopName, String shopUrl, Collection<String> excludeSysShopIds) {

        return lambdaQuery()
                .eq(StringUtils.isNotBlank(userId), ShopEntity::getUserId, userId)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .notIn(CollUtil.isNotEmpty(excludeSysShopIds), ShopEntity::getSysShopId, excludeSysShopIds)
                .nested(wrapper -> wrapper.eq(StringUtils.isNotBlank(shopName), ShopEntity::getShopName, shopName)
                        .or()
                        .eq(StringUtils.isNotBlank(shopUrl), ShopEntity::getUrl, shopUrl))
                .exists();
    }

    @Override
    public boolean checkShopExist(String userId, String shopName, String shopUrl) {
        return this.checkShopExist(userId, shopName, shopUrl, null);
    }

    @Override
    public boolean checkShopExist(String userId, String shopName, Collection<String> excludeSysShopIds) {
        return this.checkShopExist(userId, shopName, null, excludeSysShopIds);
    }

    @Override
    public boolean checkShopExist(String shopUrl) {
        return this.checkShopExist(null, null, shopUrl, null);
    }

    @Override
    public void updateShopAuthorizationStatus(String token, SuccessfulEnum successfulEnum) {
        lambdaUpdate()
                .eq(ShopEntity::getAccessToken, token)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .set(ShopEntity::getAuthorization, successfulEnum)
                .update();
    }

    @Override
    public void updateBySysShopId(String sysShopId, ShopEntity updateEntity) {
        lambdaUpdate()
                .eq(ShopEntity::getSysShopId, sysShopId)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .update(updateEntity);

    }

    @Override
    public List<ShopEntity> listBySysShopIds(List<String> sysShopIds) {

        if (CollUtil.isEmpty(sysShopIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return lambdaQuery()
                .in(ShopEntity::getSysShopId, sysShopIds)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    public ShopEntity getByShopId(String sysShopId) {
        return lambdaQuery()
                .eq(ShopEntity::getSysShopId, sysShopId)
                .eq(ShopEntity::getDataStatus, DataStatusEnum.ENABLE)
                .one();
    }
}
